//
//  DeviceDetailViewController.swift
//  XXIOT
//
//  Created by xx on 2024/8/19.
//

import UIKit
import FastSwiftSDK



class DeviceDetailViewController: FastViewController {
    
    var viewType = 0// 0添加 1查看
    var type = ""
    var typeName = ""
    var mac = ""
    var data:Dictionary<String,Any>?
    
    private lazy var lbl_txt : FastLabel = {
        let lbl = FastLabel.init(text: "",color: themeColor)
        self.view.addSubview(lbl)
        return  lbl
    } ()
    
    private lazy var txt_remark : FastTextField = {
        let txt = FastTextField.init(color: .x333,placeholder: "备注")
        self.view.addSubview(txt)
        return  txt
    } ()
    
    private lazy var btn_done : FastButton = {
        let btn =  FastButton.init(title: "保存", imgName: "",color: .white ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = themeColor
        btn.click = { [weak self] in
            self?.done()
        }
        self.view.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_task : FastButton = {
        let btn =  FastButton.init(title: "定时任务", imgName: "",color: .white ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = themeColor
//        btn.isHidden = true
        btn.click = { [weak self] in
            self?.done()
        }
        self.view.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_unbind : FastButton = {
        let btn =  FastButton.init(title: "解绑", imgName: "",color: .white ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = themeColor
        btn.click = { [weak self] in
            self?.unbind()
        }
        btn.isHidden = true
        self.view.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_delete : FastButton = {
        let btn =  FastButton.init(title: "删除", imgName: "",color: .white ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = themeColor
        btn.click = { [weak self] in
            self?.delete()
        }
        btn.isHidden = true
        self.view.addSubview(btn)
        return  btn
    } ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "设备详情"
        if let data = self.data{
            let text = "类型：\(data.stringConversion("name"))\n\nMAC:\(data.stringConversion("mac"))"
            self.lbl_txt.text = text
            self.txt_remark.text = data.stringConversion("remark")
            self.btn_delete.isHidden = false
            self.btn_unbind.isHidden = false
        }else{
            let text = "类型：\(typeName)\n\nMAC:\(mac)"
            self.lbl_txt.text = text
            if viewType == 0{
                
            }else if viewType == 1{
                btn_task.isHidden = false
            }
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: false)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        lbl_txt.snp.makeConstraints { make in
            make.top.equalTo(50+kNavH)
            make.left.equalTo(20)
        }
        txt_remark.snp.makeConstraints { make in
            make.top.equalTo(lbl_txt.snp.bottom).offset(50)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(45)
        }
        btn_done.snp.makeConstraints { make in
            make.top.equalTo(txt_remark.snp.bottom).offset(100)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(btnHeigth)
        }
        btn_task.snp.makeConstraints { make in
            make.top.equalTo(btn_done.snp.bottom).offset(20)
            make.left.right.height.equalTo(btn_done)
        }
        btn_unbind.snp.makeConstraints { make in
            make.top.equalTo(btn_task.snp.bottom).offset(20)
            make.left.right.height.equalTo(btn_done)
        }
        btn_delete.snp.makeConstraints { make in
            make.top.equalTo(btn_unbind.snp.bottom).offset(20)
            make.left.right.height.equalTo(btn_done)
        }
    }
    
    // 完成
    func done(){
        
        let data = ["name":"Test",
                    "type":"type",
                    "mac":"12345678",
                    "remark":"测试",
        
        ]
        
        NetWorkManager.shared.addDevice(data:data) { str in
            
        } fai: { str in
            
        }
        
        self.push(TaskListViewController())
    }
    
    func delete(){
        if let mac = self.data?.string("mac"){
            NetWorkManager.shared.deleteDevice(mac: mac, suc: { str in
                FastHUD.showSuc(str)
            }, fai: { str in
                FastHUD.showErr(str)
            })
        }
    }
    
    func unbind(){
        
    }
}
